<template>
    <div class="user-chat-list-container">
        <el-row type="flex" class="index-box">
            <el-col :span="5" class="height-100-percent">
                <div class="user-list-container">
                    <div class="header">用户列表</div>
                    <div class="user-list custom-scrollbar none">
                        <div :class="[userId==item.id?'user-item active flex align-items-center':'user-item flex align-items-center']"
                             v-for="(item) in userList"
                             :key="item" @click="handleChat(item.id)">
                            <div class="avatar flex align-items-center justify-content-center">
                                <el-icon size="16">
                                    <User/>
                                </el-icon>
                            </div>
                            <div class="center">
                                <div class="ip line-one-hidden">{{item.username}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </el-col>
            <el-col :span="19" class="height-100-percent">
                <router-view></router-view>
            </el-col>
        </el-row>
    </div>
</template>

<script setup>
    import {User} from '@element-plus/icons-vue';
    import {computed, onMounted, ref} from 'vue';
    import {useRouter} from 'vue-router';
    import {useStore} from 'vuex';
    import {api} from '../utils/api';
    import {service} from '../utils/service';

    const router = useRouter();
    const store = useStore();
    let userList = ref([]);
    const userId = computed(() => store.getters.getCurrentUserId);
    onMounted(async () => {
        getUserList();
    });
    const handleChat = async (id = 1) => {
        await router.push(`/index/user/detail/${id}`);
    };
    //获取用户列表
    const getUserList = () => {
        service.doGet(api['userList'])
            .then(async (response) => {
                if (response.code === 1) {
                    userList.value = userList.value.concat(response.data.userList);
                }
            }).catch((err) => {
            console.log(err);
        })
    }
</script>

<style lang="less" scoped>
    .user-chat-list-container {
        height: 100%;

        .index-box {
            height: 100%;

            .user-list-container {
                background: #F2F6FC;
                height: 100%;
                border-right: 1px solid #DCDFE6;

                .header {
                    height: 50px;
                    line-height: 50px;
                    border-bottom: 1px solid #C0C4CC;
                    background: #fff;
                }

                .user-list {
                    height: calc(100% - 51px);
                    overflow: auto;
                    padding: 10px 0;
                    -webkit-box-sizing: border-box;
                    -moz-box-sizing: border-box;
                    box-sizing: border-box;

                    .user-item {
                        padding: 10px;
                        cursor: pointer;
                        text-align: left;

                        &.active {
                            background: #DCDFE6;

                            .avatar {
                                border: 1px solid #fff;
                            }
                        }

                        &:hover {
                            background: #DCDFE6;

                            .avatar {
                                border: 1px solid #fff;
                            }
                        }

                        .avatar {
                            width: 38px;
                            height: 38px;
                            border: 1px solid #DCDFE6;
                            -webkit-border-radius: 5px;
                            -moz-border-radius: 5px;
                            border-radius: 5px;
                            position: relative;

                            .point {
                                position: absolute;
                                width: 8px;
                                height: 8px;
                                display: block;
                                background: red;
                                -webkit-border-radius: 50%;
                                -moz-border-radius: 50%;
                                border-radius: 50%;
                                right: -4px;
                                top: -4px;
                            }
                        }

                        .center {
                            margin-left: 10px;
                            font-size: 12px;

                            .ip {
                                font-weight: 600;
                                max-width: 150px;
                            }
                        }
                    }
                }
            }
        }
    }
</style>